﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace AMSReportBuildService
{
    //资产分布采集类，按部门划分
    class AssetDistributeXmlFile
    {
        GetDataSetHelp m_Help = new GetDataSetHelp();
        internal void CreateXmlFile(string path)
        {
           var ds = m_Help.AssetDistributeData;
            if(ds != null && ds.Tables[0].Rows.Count > 0)
            {
                //初始化XML文档操作类
                var myXml = new XmlDocument();

                //建立Xml的定义声明
                XmlDeclaration dec = myXml.CreateXmlDeclaration("1.0", "GB2312", null);
                myXml.AppendChild(dec);

                XmlElement rootNode = myXml.CreateElement("chart");
                rootNode.SetAttribute("caption", "资产分布报表");
                rootNode.SetAttribute("xAxisName", "部门");
                rootNode.SetAttribute("yAxisName", "数量");
                rootNode.SetAttribute("showValues", "1");
                rootNode.SetAttribute("decimals", "0");
                rootNode.SetAttribute("formatNumberScale", "0");
                rootNode.SetAttribute("baseFontSize", "12");
                 myXml.AppendChild(rootNode);
                 foreach (DataRow row in ds.Tables[0].Rows)
                 {
                     XmlElement ele0 = myXml.CreateElement("set");
                     ele0.SetAttribute("label", row["Name"].ToString());
                     ele0.SetAttribute("value", row["count"].ToString());
                     string toolText;
                     toolText = row["count"] + "，" + (Convert.ToDouble(row["count"].ToString()) / GetDataSetHelp.GetAssetTotalCount()).ToString("0.00%");
                     ele0.SetAttribute("tooltext", toolText);
                     if (myXml.DocumentElement != null) myXml.DocumentElement.AppendChild(ele0);
                 }
                 if (File.Exists(path))
                     File.Delete(path);
                 myXml.Save(path);
            }
        }
    }
}
